<?php

/*
采购订单列表
*/
namespace Modules\Salesmanagement\Http\Repositories;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Catch\Exceptions\FailedException;
use Modules\Base\Service\BoxService;
use Modules\Promanagement\Models\Processflow;
use Modules\Promanagement\Models\Provflowsional;
use extend\services\warehouse\{warehouseService};//引入自定义类
use Modules\Purchasing\Http\Repositories\PurchaseorderReget;
use Modules\Salesmanagement\Models\SalesoutAttached;
use Modules\Salesmanagement\Models\Salesout;
use Modules\Salesmanagement\Models\SalesoutTiaoma;
use App\Services\InventoryService;
use App\Services\KingDeeService;
use App\Enums\InvTransDocType;
use Modules\Purchasing\Models\PurchasewarehousingMateriel;
use Modules\Purchasing\Models\Purchasewarehousing;
use Modules\Systemsetting\Models\Systemsetting;

class SalesmanagementRequest
{


    /*
  数据列表
  */
    public static function list($data = [])
    {
        // return self::getBuilder($data);
        return self::getBuilder($data)->paginate($data['per_page'] ?? 20);
    }

    /**
     *查询列表
     *
     * @param array $data
     * @return Builder
     */
    public static function getBuilder(array $data = []): Builder
    {

        // $builder = Provisional::query()->with('detailscheckAll');
        $builder = Salesout::query();
        if (isset($data['fbillno'])) {
            $builder->where('fbillno', 'like', "%{$data['fbillno']}%");
        }

        if (isset($data['fcustomerid_number'])) {
            $builder->where('fcustomerid_number', 'like', "%{$data['fcustomerid_number']}%");
        }

        if (isset($data['state'])) {
            $builder->where('state',$data['state']);
        }
        
        if (isset($data['writestate'])) {
            $builder->where('writestate',$data['writestate']);
        }

        $builder->orderBy('id', 'desc');
        $builder->get()->toArray();
        return $builder;
    }

    /*
    列表详情接口
    */
    public static function SalesdetailsData($id)
    {

        $data = SalesoutAttached::where('salesout_id',$id)
            ->get()
            ->toArray();
        return ['code'=>1000,'message'=>'请求成功','data'=>$data];
    }

    /*
    数据添加方法
    */
    public static function create(array $data)
    {

        $Salesout = new Salesout;
        return self::createOrSave($Salesout, $data);
    }

    //数据添加方法
    public static function createOrSave(Salesout $Salesout,$data)
    {


        try {
            //开始起事务
            DB::beginTransaction();

            $Salesoutdata = [];
            $Salesoutdata['fid'] = $data['fid'];
            $Salesoutdata['fbillno'] = $data['fbillno'];
            $Salesoutdata['fsaleorgid_number'] = $data['fsaleorgid_number'];
            $Salesoutdata['fsaleorgid_name'] = $data['fsaleorgid_name'];
            $Salesoutdata['fcustomerid_number'] = $data['fcustomerid_number'];
            $Salesoutdata['fcustomerid_name'] = $data['fcustomerid_name'];
            $Salesoutdata['fdocumentstatus'] = $data['fdocumentstatus'];
            $Salesoutdata['fpurchaseorgid_number'] = $data['fpurchaseorgid_number'];
            $Salesoutdata['fpurchaseorgid_name'] = $data['fpurchaseorgid_name'];
            $Salesoutdata['fsupplierid_number'] = $data['fsupplierid_number'];
            $Salesoutdata['fsupplierid_name'] = $data['fsupplierid_name'];
            $Salesoutdata['incomingtype'] = $data['incomingtype'];

            // $Salesoutdata['fownertypeid'] = $data['fownertypeid'];
            // $Salesoutdata['fownerid_number'] = $data['fownerid_number'];
            // $Salesoutdata['fownerid_name'] = $data['fownerid_name'];

            $Salesout->fill($Salesoutdata);
            $Salesout->save();


            $Salesout->detailscheckAll()->createMany( $data['FEntry'] );

            DB::commit();
            return ['code'=>1000,'message'=>'提交成功'];
        } catch (\Exception $e) {
            DB::rollBack();
            throw new FailedException($e);
        }
    }

    //修改
    public static function createUpdate($fid,$data)
    {


        $datares  = Salesout::query()->select('id')->where('fid',$fid)->first();
        if(empty( $datares)){throw new FailedException('亲，修改数据失败，没有该单号。');}
        try {
            //开始起事务
            DB::beginTransaction();
            $Salesout = Salesout::query()->updateOrCreate(['fid' => $fid], [
                'fbillno'       => $data['fbillno']  ?? '',
                'fsaleorgid_number'       => $data['fsaleorgid_number']  ?? '',
                'fsaleorgid_name'       => $data['fsaleorgid_name']  ?? '',
                'fcustomerid_number'       => $data['fcustomerid_number']  ?? '',
                'fcustomerid_name'       => $data['fcustomerid_name']  ?? '',
                'fdocumentstatus'       => $data['fdocumentstatus']  ?? '',
            ]);
             $SalesoutAttached = SalesoutAttached::query()->select('*')->where('fid',$fid)->get()->toArray();
            
                //删除成功
             $result = [];
             $fentryid = [];
             $absent = [];
             foreach($data['FEntry'] as $k=>$v){
                 $fentryid[] = $v['fentryid'];
             }
             $SalesoutAttached_fentryid = [];
              foreach($SalesoutAttached as $k=>$v){
                 $SalesoutAttached_fentryid[] = $v['fentryid'];
             }
             foreach($SalesoutAttached as $key => $val ){
                 if(in_array($val['fentryid'], $fentryid)){
                      array_push($result, $val['fentryid']);
                 }else{
                     //删除数据库匹配不了的值
                   $absent[] = $val['fentryid'];
                 }
             }
             
             $data_SalesoutAttached = [];
             $absent_fentryid = [];
             foreach($data['FEntry'] as $key => $val ){
                 if(in_array($val['fentryid'], $SalesoutAttached_fentryid)){
                      array_push($data_SalesoutAttached, $val['fentryid']);
                 }else{
                     //新加匹配不了的的数据库
                   $absent_fentryid[] = $val['fentryid'];
                 }
             }
             
             if(!empty($result)){
                  //要修改的数据
                $resultdata = [];
                foreach ($result as $k=>$v){
                    $resultdata[$v] = $v;
                }
             }
        
            //  p($absent);
             //要删的数据
             if(!empty($absent)){
                 $deledata = [];
                 foreach ($absent as $k=>$v){
                    // $deledata[$v] = $v;
                      $SalesoutAttached = SalesoutAttached::query()->select('id')->where('salesout_id',$Salesout['id'])->where('fentryid',$v)->first();
                    SalesoutAttached::query()->where('id',$SalesoutAttached['id'])->delete();
                 }   

             }
             
         
             
             //要删添加的数据
             if(!empty($absent_fentryid)){
               $Addeddata = [];
                foreach ($absent_fentryid as $k=>$v){
                    $Addeddata[$v] = $v;
                }  
             }
             
             

            foreach($data['FEntry'] as $k=>$v){
                //修改的数据
                if(!empty($resultdata)){
                    if(isset($resultdata[$v['fentryid']])){
                            $SalesoutAttached = SalesoutAttached::query()->select('id')->where('salesout_id',$Salesout['id'])->where('fid',$fid)->where('fentryid',$v['fentryid'])->first();
                            if(empty($SalesoutAttached)){
                                throw new FailedException('单据体出错');
                            }
                            SalesoutAttached::query()->where('id',$SalesoutAttached['id'])->update(
                            [
                                
                                'fid'=>$v['fid'],
                                'fseq'=>$v['fseq'],
                                'fmaterialid'=>$v['fmaterialid'],
                                'fmaterialname'=>$v['fmaterialname'],
                                'fmaterialmodel'=>$v['fmaterialmodel'],
                                'fmodel'=>$v['fmodel'],
                                'funitid_number'=>$v['funitid_number'],
                                'funitid_name'=>$v['funitid_name'],
                                'fqty'=>$v['fqty'],
                                'fstockid_number'=>$v['fstockid_number'],
                                'fstockid_name'=>$v['fstockid_name'],
                                'fmemo'=>$v['fmemo'],
                                'fownertypeid'=>$v['fownertypeid'],
                                'fownerid_number'=>$v['fownerid_number'],
                                'fownerid_name'=>$v['fownerid_name'],
                            ]
                            );
                    }
                }
                 
                // //删除的数据
                // if(!empty($deledata)){
                //   if(isset($deledata[$v['fentryid']])){
                //     $SalesoutAttached = SalesoutAttached::query()->select('id')->where('salesout_id',$Salesout['id'])->where('fentryid',$v['fentryid'])->first();
                   
                //     if(empty($SalesoutAttached)){
                //         throw new FailedException('单据体出错');
                //     }
                //     SalesoutAttached::where('id',$SalesoutAttached['id'])->delete();
                //     }
                // }
                 
                 //新加的数据
                 if(!empty($Addeddata)){
                       if(isset($Addeddata[$v['fentryid']])){
                        
                        
                        SalesoutAttached::insertGetId(
                             [
                                'salesout_id'=>$Salesout['id'],
                                'fid'=>$v['fid'],
                                'fseq'=>$v['fseq'],
                                'fentryid'=>$v['fentryid'],
                                'fmaterialid'=>$v['fmaterialid'],
                                'fmaterialname'=>$v['fmaterialname'],
                                'fmaterialmodel'=>$v['fmaterialmodel'],
                                'fmodel'=>$v['fmodel'],
                                'funitid_number'=>$v['funitid_number'],
                                'funitid_name'=>$v['funitid_name'],
                                'fqty'=>$v['fqty'],
                                'fstockid_number'=>$v['fstockid_number'],
                                'fstockid_name'=>$v['fstockid_name'],
                                'fmemo'=>$v['fmemo'],
                                'fownertypeid'=>$v['fownertypeid'],
                                'fownerid_number'=>$v['fownerid_number'],
                                'fownerid_name'=>$v['fownerid_name'],
                                'created_at'=>time(),
                                'updated_at'=>time(),
                            ]
                            );
                     }
                 }
                
                
            }

            // SalesoutAttached::where('salesout_id',$id)->delete();
            // $Salesout->detailscheckAll()->createMany($data['FEntry']);

            DB::commit();
            return ['code'=>1000,'message'=>'修改成功'];
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }

    }




    /**
     * @param $id
     * @return void
     */
    public static function delete($fid)
    {

        try {
            //开始起事务
            DB::beginTransaction();
            Salesout::query()->where('fid',$fid)->delete();
            $Provisionalschedule_id= SalesoutAttached::where('salesout_id',$fid)->first()->toArray();
            DB::commit();
            return '删除成功';
        } catch (\Exception $e) {
            DB::rollBack();
            throw new FailedException('服务器飞出地球了请联系技术找回');
        }
    }

    //数量录入方法和金蝶推送
    public static function pdassanningaddData($data)
    {
       

        $Salesout = Salesout::query()->select(
            'fid',
            'writestate',
            'fbillno',
            'fsaleorgid_number',
            'fsaleorgid_name',
            'incomingtype',
            'fpurchaseorgid_number',
            'fpurchaseorgid_name',
            'fsupplierid_number',
            'fsupplierid_name',
            )->find($data['salesout_id']);
        if(empty($Salesout)){
            throw new FailedException('销售单数据查询出错');
        }
        
        if($Salesout['writestate']==1){
            throw new FailedException('该销售出库单已回传金蝶无需在扫描');
        }

        //提交的数量和销售数量判断
        $count = 0;
        foreach($data['data'] as $k=>$v){
            $count +=$v['quantity'];

        }

        $SalesoutAttached = SalesoutAttached::query()
            ->select(DB::raw('SUM(fqty) as fqty'))
            ->where('salesout_id',$data['salesout_id'])
            ->groupby(['salesout_id'])
            ->first();

        if($count != $SalesoutAttached['fqty']){
            throw new FailedException('销售数量没有和计划销售数据量不一致，无法提交！');
        }

        try {
            //开始起事务
            DB::beginTransaction();
            $value = InvTransDocType::SALES_DELIVERY->value;

            $InterorgOpening = Systemsetting::Opening('*',['argument_code'=>'Saleout']);    //组织间生成采购单入库单是否开启
        
            //组织间销售出库生成采购入库单数据数据
            if(!empty($InterorgOpening)){
                if($Salesout['incomingtype']==1){
                    $Purchasewdata = [];
                    $Purchasewdata['warehosnumber'] = \App\Services\UnitService::createDocno(\App\Enums\OrderNoPrefix::NO_ZZCG->value);
                    $Purchasewdata['creator_id'] = $data['creator_id'];//创建人
                    $Purchasewdata['remarks'] = '';
                    $Purchasewdata['incomingtype'] = 1;
                    $Purchasewdata['fbillno'] = $Salesout->fbillno;
                    $Purchasewdata['fpurchaseorgid_number'] = $Salesout->fpurchaseorgid_number;
                    $Purchasewdata['fpurchaseorgid_name'] = $Salesout->fpurchaseorgid_name;
                    $Purchasewdata['fsupplierid_number'] = $Salesout->fsupplierid_number;
                    $Purchasewdata['fsupplierid_name'] = $Salesout->fsupplierid_name;
                    $Purchasewdata['fid'] = $Salesout->fid;
                    $Purchasewarehousing_id = Purchasewarehousing::insertGetId($Purchasewdata);
                    if(empty($Purchasewarehousing_id)){
                        return throw new FailedException('亲！数据出错,无法销售出库');
                    }
                }
            }
            
            foreach($data['data'] as $k=>$v){

                $SalesoutTiaoma = [
                    'salesout_id'=>$data['salesout_id'],
                    'salesout_attached_id'=>$v['salesout_attached_id'],
                    'quantity'=>$v['quantity'],
                    'barcode'=>$v['barcode'],
                ];
                SalesoutTiaoma::insert($SalesoutTiaoma);


                $SalesoutAttached_data = SalesoutAttached::query()
                    ->select(
                        'salesout_id',
                        'fid',
                        'fseq',
                        'fentryid',
                        'fmaterialid',
                        'fmaterialname',
                        'fmaterialmodel',
                        'fmodel',
                        'funitid_number',
                        'funitid_name',
                        'fqty',
                        'fstockid_number',
                        'fstockid_name',
                        'fownertypeid',
                        'fownerid_number',
                        'fownerid_name',
                )
                    ->where('salesout_id',$data['salesout_id'])
                    ->where('id',$v['salesout_attached_id'])
                    ->where('fmaterialid',$v['fmaterialid'])
                    ->first();

                if(empty($SalesoutAttached_data)){
                    return throw new FailedException('亲！数据出错,无法获取物料信息');
                }

                if(!empty($InterorgOpening)){
                //组织间销售出库生成采购入库单数据数据
                    if($Salesout['incomingtype']==1){
                        $purchasewarehousing_materiel[$k]['purchasewarehousing_id'] = $Purchasewarehousing_id;
                        $purchasewarehousing_materiel[$k]['fid'] = $SalesoutAttached_data->fid;
                        $purchasewarehousing_materiel[$k]['fseq'] = $SalesoutAttached_data->fseq;
                        $purchasewarehousing_materiel[$k]['fentryid'] = $SalesoutAttached_data->fentryid;
                        $purchasewarehousing_materiel[$k]['fmaterialid'] = $SalesoutAttached_data->fmaterialid;
                        $purchasewarehousing_materiel[$k]['fmaterialname'] = $SalesoutAttached_data->fmaterialname;
                        $purchasewarehousing_materiel[$k]['fmaterialmodel'] = $SalesoutAttached_data->fmaterialmodel;
                        $purchasewarehousing_materiel[$k]['fmodel'] = $SalesoutAttached_data->fmodel;
                        $purchasewarehousing_materiel[$k]['funitid_number'] = $SalesoutAttached_data->funitid_number;
                        $purchasewarehousing_materiel[$k]['funitid_name'] = $SalesoutAttached_data->funitid_name;
                        $purchasewarehousing_materiel[$k]['fqty'] = $v['quantity'];
                        $purchasewarehousing_materiel[$k]['fstockid_number'] = $SalesoutAttached_data->fstockid_number;
                        $purchasewarehousing_materiel[$k]['fstockid_name'] = $SalesoutAttached_data->fstockid_name;
                        /*$purchasewarehousing_materiel[$k]['fownertypeid'] = $SalesoutAttached_data->fownertypeid;
                        $purchasewarehousing_materiel[$k]['fownerid_number'] = $SalesoutAttached_data->fownerid_number;
                        $purchasewarehousing_materiel[$k]['fownerid_name'] = $SalesoutAttached_data->fownerid_name;*/
                        $purchasewarehousing_materiel[$k]['fownertypeid'] = 'BD_OwnerOrg';
                        $purchasewarehousing_materiel[$k]['fownerid_number'] = $Salesout->fpurchaseorgid_number;
                        $purchasewarehousing_materiel[$k]['fownerid_name'] = $Salesout->fpurchaseorgid_name;
                        $purchasewarehousing_materiel[$k]['salesout_attached_id'] = $SalesoutAttached_data->id;
                        
                        $value = InvTransDocType::SALES_DELIVERY_TISSUE->value;
                    }
                }
                InventoryService::decInventory(
                    $SalesoutAttached_data->fownerid_number,
                    $v['fbillno'],
                    $data['salesout_id'],
                    $value,
                    $v['fmaterialid'],
                    $v['warehousecode'],
                    $v['barcode'],
                    $v['quantity'],
                    $SalesoutAttached_data->fownerid_number,
                );

            }
            
            if(!empty($InterorgOpening)){
                        if($Salesout['incomingtype']==1){
                                        $SalesoutAttached_data_res = SalesoutAttached::query()
                                            ->select(
                                            'salesout_id',
                                            'fid',
                                            'fseq',
                                            'fentryid',
                                            'fmaterialid',
                                            'fmaterialname',
                                            'fmaterialmodel',
                                            'fmodel',
                                            'funitid_number',
                                            'funitid_name',
                                            'fqty',
                                            'fstockid_number',
                                            'fstockid_name',
                                            'fownertypeid',
                                            'fownerid_number',
                                            'fownerid_name',
                                            'id'
                                            )
                                        ->where('salesout_id',$data['salesout_id'])
                                        ->get();
                                        // ->toArry();
                                        //录入推送数据
                                        $purchasewarehousing_materiel = [];
                                        foreach($SalesoutAttached_data_res as $k=>$v){
                                            //组织间销售出库生成采购入库单数据数据
                                            if($Salesout['incomingtype']==1){
                                                $purchasewarehousing_materiel[$k]['purchasewarehousing_id'] = $Purchasewarehousing_id;
                                                $purchasewarehousing_materiel[$k]['fid'] = $v['fid'];
                                                $purchasewarehousing_materiel[$k]['fseq'] = $v['fseq'];
                                                $purchasewarehousing_materiel[$k]['fentryid'] = $v['fentryid'];
                                                $purchasewarehousing_materiel[$k]['fmaterialid'] = $v['fmaterialid'];
                                                $purchasewarehousing_materiel[$k]['fmaterialname'] = $v['fmaterialname'];
                                                $purchasewarehousing_materiel[$k]['fmaterialmodel'] = $v['fmaterialmodel'];
                                                $purchasewarehousing_materiel[$k]['fmodel'] = $v['fmodel'];
                                                $purchasewarehousing_materiel[$k]['funitid_number'] = $v['funitid_number'];
                                                $purchasewarehousing_materiel[$k]['funitid_name'] = $v['funitid_name'];
                                                $purchasewarehousing_materiel[$k]['fqty'] = $v['fqty'];
                                                $purchasewarehousing_materiel[$k]['fstockid_number'] = $v['fstockid_number'];
                                                $purchasewarehousing_materiel[$k]['fstockid_name'] = $v['fstockid_name'];
                                                /*$purchasewarehousing_materiel[$k]['fownertypeid'] = $v['']fownertypeid;
                                                $purchasewarehousing_materiel[$k]['fownerid_number'] = $v['']fownerid_number;
                                                $purchasewarehousing_materiel[$k]['fownerid_name'] = $v['']fownerid_name;*/
                                                $purchasewarehousing_materiel[$k]['fownertypeid'] = 'BD_OwnerOrg';
                                                $purchasewarehousing_materiel[$k]['fownerid_number'] = $Salesout->fpurchaseorgid_number;
                                                $purchasewarehousing_materiel[$k]['fownerid_name'] = $Salesout->fpurchaseorgid_name;
                                                $purchasewarehousing_materiel[$k]['salesout_attached_id'] = $v['id'];
                                            }
                                        }
                        }
                        //组织间销售出库生成采购入库单数据数据
                        if($Salesout['incomingtype']==1){
                            $PurchasewarehousingMateriel = PurchasewarehousingMateriel::insert($purchasewarehousing_materiel);
                            if(empty($PurchasewarehousingMateriel)){
                                return throw new FailedException('数据出做无法录入');
                            }
                        }
            }

            //金蝶推送
            $order = [
                'FID'=>$Salesout->fid,
                'F_TVJB_WMSSMJL'=>"YSM",
            ];
            (new KingDeeService())->form('SAL_OUTSTOCK')->invokeSave($order,['NeedUpDateFields'=>['FID','F_TVJB_WMSSMJL']]);

          
            Salesout::where('id',$data['salesout_id'])->update(
                [
                    'state'=>1,
                    'writestate'=>1,
                ]
            );

            DB::commit();
            return ['code'=>1000,'message'=>'提交成功'];
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }

    }

    //采购入库单->生成采购入库数据
    // public static function purchasereceipt($id,$data)
    // {


    //     try {
    //         //开始起事务
    //         DB::beginTransaction();
    //         SalesoutTiaoma::where('id',$id)->update($data);
    //         DB::commit();
    //         return;
    //     } catch (\Exception $e) {
    //         DB::rollBack();
    //         throw new FailedException($e);
    //     }

    // }

    //wms 数量修改
    public static function Modify($id,$data)
    {


        try {
            //开始起事务
            DB::beginTransaction();
            SalesoutTiaoma::where('id',$id)->update($data);
            DB::commit();
            return;
        } catch (\Exception $e) {
            DB::rollBack();
            throw new FailedException($e);
        }

    }


    //wms 详情接口
    public static function DetailinterfaceData($id,$data)
    {

        $Salesout = Salesout::where('id',$id)->first();

        if(!isset($data['all'])){

            $datares = SalesoutTiaoma::select('salesout_id','salesout_attached_id','quantity','barcode')->where('salesout_id',$id)->get()->toArray();
            foreach($datares as $k=>$v){
                $SalesoutAttached = SalesoutAttached::where('salesout_id',$v['salesout_id'])->where('id',$v['salesout_attached_id'])->first();
                if(empty($SalesoutAttached)){
                    throw new FailedException('数据出错');
                }
                $datares[$k]['fmaterialid'] = $SalesoutAttached->fmaterialid;
                $datares[$k]['fmaterialname'] = $SalesoutAttached->fmaterialname;
                $datares[$k]['fmaterialmodel'] = $SalesoutAttached->fmaterialmodel;
                $datares[$k]['fmodel'] = $SalesoutAttached->fmodel;
                $datares[$k]['funitid_number'] = $SalesoutAttached->funitid_number;
                $datares[$k]['funitid_name'] = $SalesoutAttached->funitid_name;
                $datares[$k]['fstockid_number'] = $SalesoutAttached->fstockid_number;
                $datares[$k]['fstockid_name'] = $SalesoutAttached->fstockid_name;
            }
            // $datares = $datares->select('*',DB::raw('SUM(fqty) as fqty'),'storagelocation','inspectionlist_id','purchaseorderschedule_id');
            // $datares = $datares->groupby(['supplierschedule_id','provisional_id','storagelocation','inspectionlist_id','purchaseorderschedule_id']);
        }else{
            $datares = SalesoutAttached::query()
                ->select(DB::raw('SUM(fqty) as fqty'),'fmaterialid','fmaterialname','fmaterialmodel','fmodel','funitid_number','funitid_name','fstockid_number','fstockid_name')
                ->where('salesout_id',$id)
                ->groupby(['salesout_id','fmaterialid','fmaterialname','fmaterialmodel','fmodel','funitid_number','funitid_name','fstockid_number','fstockid_name']);
            $datares = $datares->get()->toArray();
            
            foreach($datares as $k=>$v){
                $data_res = SalesoutAttached::where('salesout_id',$id)
                ->where('fmaterialid',$v['fmaterialid'])
                ->where('fmaterialname',$v['fmaterialname'])
                ->where('fmaterialmodel',$v['fmaterialmodel'])
                ->where('fmodel',$v['fmodel'])
                ->where('funitid_number',$v['funitid_number'])
                ->where('funitid_name',$v['funitid_name'])
                ->where('fstockid_number',$v['fstockid_number'])
                ->first();
                $datares[$k]['salesout_id'] = $data_res['salesout_id'];
                $datares[$k]['fentryid'] = $data_res['fentryid'];
                $datares[$k]['fseq'] = $data_res['fseq'];
                $datares[$k]['fid'] = $data_res['fid'];

            }
                
        }

        $Salesout['detailscheck_all'] = $datares;


        return ['code'=>1000,'message'=>'请求成功','data'=>$Salesout];

    }

    //wms 金蝶审核和推送接口
    public static function Kingdeeaudit($id,$data)
    {

        try {
            //开始起事务
            DB::beginTransaction();
            SalesoutAttached::where('id',$id)->update(['state'=>1]);
            DB::commit();
            return;
        } catch (\Exception $e) {
            DB::rollBack();
            throw new FailedException($e);
        }



    }






}
